package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.Category;
import com.atguigu.ssyx.product.service.CategoryService;
import com.atguigu.ssyx.vo.product.CategoryQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-10-30
 */
@Api(tags = "商品分类")
@RestController
@RequestMapping("/admin/product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    // 商品分类列表
    @ApiOperation("商品分类列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        CategoryQueryVo categoryQueryVo) {
        Page<Category> pageParam = new Page<>(page, limit);
        IPage<Category> pageModel = categoryService.selectPageCategory(pageParam, categoryQueryVo);
        return Result.ok(pageModel);
    }

    // 根据id，查询商品分类
    @ApiOperation("根据id，查询商品分类")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    // 获取商品所有分类
    @ApiOperation("获取商品所有分类")
    @GetMapping("findAllList")
    public Result findAllList() {
       return Result.ok(categoryService.findAllList());
    }

    // 添加商品分类
    @ApiOperation("添加商品分类")
    @PostMapping("save")
    public Result save(@RequestBody Category category) {
        boolean save = categoryService.save(category);
        return Result.ok(save);
    }

    // 修改商品分类
    @ApiOperation("修改商品分类")
    @PutMapping("update")
    public Result updateById(@RequestBody Category category) {
        boolean update = categoryService.updateById(category);
        return Result.ok(update);
    }

    // 根据id删除商品分类
    @ApiOperation("根据id删除商品分类")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean remove = categoryService.removeById(id);
        return Result.ok(remove);
    }

    // 根据id批量删除商品分类
    @ApiOperation("根据id批量删除商品分类")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        boolean remove = categoryService.removeByIds(idList);
        return Result.ok(remove);
    }
}

